package com.easy.cloud.web.service.upms.biz.repository;

import com.easy.cloud.web.component.mysql.repository.JpaLogicRepository;
import com.easy.cloud.web.service.upms.biz.domain.UserDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * User持久化
 *
 * @author Fast Java
 * @date 2023-08-03 14:25:41
 */
@Repository
public interface UserRepository extends JpaLogicRepository<UserDO, String>, JpaSpecificationExecutor<UserDO> {

    /**
     * 根据账号或电话获取用户信息
     *
     * @param userName 账号
     * @param tel      电话
     * @return java.util.List<com.easy.cloud.web.service.upms.biz.domain.UserDO>
     */
    List<UserDO> findByUserNameOrTel(String userName, String tel);

    /**
     * 根据账号获取用户信息
     *
     * @param userName 账号信息
     * @return java.util.Optional<com.easy.cloud.web.service.upms.biz.domain.UserDO>
     */
    Optional<UserDO> findByUserName(String userName);

    /**
     * 匹配数据，任意一个条件符合即可
     *
     * @param unionId 唯一标识
     * @param appleId 唯一标识
     * @param tel     唯一标识
     * @return java.util.List<com.easy.cloud.web.service.upms.biz.domain.UserDO>
     */
    List<UserDO> findByUnionIdOrAppleIdOrTel(String unionId, String appleId, String tel);

    /**
     * 匹配数据，任意一个条件符合即可
     *
     * @param tel 电话
     * @return java.util.Optional<com.easy.cloud.web.service.upms.biz.domain.UserDO>
     */
    Optional<UserDO> findByTel(String tel);

    /**
     * 冻结租户下所有用户信息
     *
     * @param tenantId 租户信息
     */
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "UPDATE db_user table SET table.deleted = 'DELETED' WHERE table.tenant_id = ?1", nativeQuery = true)
    void logicDeleteTenantAllUser(String tenantId);

    /**
     * 冻结租户下所有用户信息
     *
     * @param tenantId 租户信息
     * @param status   用户状态
     */
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "UPDATE db_user table SET table.status = ?2 WHERE table.tenant_id = ?1", nativeQuery = true)
    void updateTenantAllUser(String tenantId, String status);
}